package com.leaves.javamall.product.service.impl;

import com.leaves.javamall.product.entity.CategoryBrandRelationEntity;
import com.leaves.javamall.product.service.CategoryBrandRelationService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leaves.common.utils.PageUtils;
import com.leaves.common.utils.Query;

import com.leaves.javamall.product.dao.BrandDao;
import com.leaves.javamall.product.entity.BrandEntity;
import com.leaves.javamall.product.service.BrandService;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
    @Autowired
    BrandService brandService;
    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //获取从前端页面传来的参数
        String key = (String) params.get("key");
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        //判断传入的参数是否为空
        if(!StringUtils.isNullOrEmpty(key)){
            //新建一个queryWrapper来搜索
            wrapper.eq("brand_id", key).or().like("name", key);
        }
//      封装成page传回前端
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void updateDetails(BrandEntity brand) {
        this.updateById(brand);
        if(!StringUtils.isNullOrEmpty(brand.getName())){
            System.out.println("判断品牌为非空"+StringUtils.isNullOrEmpty(brand.getName()));
            categoryBrandRelationService.updateBrandDetails(brand.getBrandId(),brand.getName());
        }
    }

}